package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotSensorRecordService;
import com.lechi.web.system.domain.IotSensorRecordBo;
import com.lechi.web.system.mapper.iot.IotSensorRecordMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 传感器数据上传记录Service业务层处理
 *
 * @author yangguang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotSensorRecordServiceImpl implements IIotSensorRecordService {

    private final IotSensorRecordMapper IotSensorRecordMapper;


    /**
     * 查询设备传感器数据上传记录
     *
     * @param IotSensorRecordBo 传感器数据上传记录
     * @return 传感器数据上传记录
     */
    @Override
    public List<IotSensorRecordBo> selectIotSensorRecordList(IotSensorRecordBo IotSensorRecordBo) {
        try {
            return IotSensorRecordMapper.selectIotSensorRecordList(IotSensorRecordBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询设备传感器数据上传记录", method, IotSensorRecordBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 设备传感器数据上传记录
     *
     * @param IotSensorRecordBo 设备传感器数据上传记录
     */
    @Override
    public void insertIotSensorRecord(IotSensorRecordBo IotSensorRecordBo) {
        try {
            IotSensorRecordMapper.insertIotSensorRecord(IotSensorRecordBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "站点生成数据文件后添加记录异常", method, IotSensorRecordBo));
            log.error("站点生成数据文件后添加记录异常", e);
        }
    }

    /**
     * 删除设备传感器数据
     *
     * @param IotSensorRecordBo 设备上传图片
     */
    @Override
    public void deleteIotSensorRecordByParams(IotSensorRecordBo IotSensorRecordBo) {
        try {
            IotSensorRecordMapper.deleteIotSensorRecordByParams(IotSensorRecordBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "删除设备传感器数据记录", method, IotSensorRecordBo));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    /**
     * 批量删除设备传感器数据
     *
     * @param ids 需要删除的设备上传图片主键
     */
    @Override
    public void deleteIotSensorRecordByIds(Long[] ids) {
        try {
            IotSensorRecordMapper.deleteIotSensorRecordByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除设备传感器数据记录", method, ids));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

}
